# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, String, Integer, Enum
import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATABASE = "sqlite:///{}".format(os.path.join(BASE_DIR, 'book.db3'))
Engine = create_engine(DATABASE, echo=False)
Session = sessionmaker(bind=Engine)
Base = declarative_base()


class User(Base):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(20), nullable=False)
    password = Column(String(20), nullable=False)

    def __init__(self, name, password):
        self.name = name
        self.password = password


class BookManager(Base):
    __tablename__ = "book"

    id = Column(Integer, primary_key=True, autoincrement=True)
    b_name = Column(String(20), nullable=False)
    b_price = Column(String(20), nullable=False)
    b_author = Column(String(20), nullable=False)
    b_sex = Column(Enum("男", "女"), nullable=False)
    b_type = Column(String(20), nullable=False)
    b_desc = Column(String(100), nullable=True)

    def __init__(
            self,
            b_name,
            b_price,
            b_author,
            b_sex,
            b_type,
            b_desc
    ):
        self.b_name = b_name
        self.b_price = b_price
        self.b_author = b_author
        self.b_sex = b_sex
        self.b_type = b_type
        self.b_desc = b_desc


class BookType(Base):
    __tablename__ = "book_type"

    id = Column(Integer, primary_key=True, autoincrement=True)
    b_type = Column(String(20), nullable=False)
    b_desc = Column(String(100), nullable=True)

    def __init__(self, b_type, b_desc):
        self.b_type = b_type
        self.b_desc = b_desc


if __name__ == '__main__':
    User.metadata.create_all(bind=Engine)
    BookManager.metadata.create_all(bind=Engine)
    BookType.metadata.create_all(bind=Engine)
